package com.fingard.dsp.bank.directbank.gynsh01;

import com.alibaba.fastjson.JSONObject;
import com.fingard.constant.Format;
import com.fingard.dsp.bank.directbank.gynsh01.util.JsonHelper;
import com.fingard.dsp.bank.directbank.bankInterface.IBalance;
import com.fingard.dsp.bank.reqrespobj.balance.Req7505HisBalQry;
import com.fingard.dsp.bank.reqrespobj.balance.Req7506BalQry;
import com.fingard.dsp.bank.reqrespobj.balance.RetAts7505HisBalQry;
import com.fingard.dsp.bank.reqrespobj.balance.RetAts7506BalQry;
import com.fingard.dsp.bank.reqrespobj.balance.detail.RetAtsBalDetail;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @Description TODO
 * @Author xtt
 * @Date $ $
 **/
public class GYNSH01Balance extends GYNSH01Base implements IBalance {
    @Override
    public RetAts7506BalQry d_7506_BalQry(Req7506BalQry p_req7506) throws Exception {
        RetAts7506BalQry retAts = new RetAts7506BalQry(p_req7506.reqHeader);
        retAts.writeStart();
        //JSON文本内容发送
        JSONObject obj = new JSONObject();
        SimpleDateFormat yyyyMMdd = new SimpleDateFormat("yyyyMMdd");
        obj.put("externalLocalDateText", yyyyMMdd.format(new Date()));// 当前日期	8	S	M	YYYYMMDD 北京时间
        obj.put("transDate",yyyyMMdd.format(new Date()));
        obj.put("transTime",Format.DateTime14Format.format(new Date()));
        //目前只支持单个账户查询余额
        while (p_req7506.readRecord()) {
            obj.put("tfAcctNo", p_req7506.readedDetail.actNum);// 账号	32	S	M
        }
        obj.put("tfAcSeqn", "0");// 账户序号	10	I	O
        System.out.println(obj);
        JSONObject bizCont = JsonHelper.createBankBizCont(obj,getBankConfig().getValueAsString("privateKeyMerchant"),getBankConfig().getValueAsString("publicKeyBank"));
        JSONObject headObj = createHead("gyns.tht.acct.query");
        headObj.put("biz_content", bizCont);
        RetAtsBalDetail detail = new RetAtsBalDetail();

        String retStrArr = sendToBank(headObj.toJSONString());
            if (retStrArr.length() > 0) {
                JSONObject json = JSONObject.parseObject(retStrArr);

                if (json != null) {

                    String tmpRetCode = json.getString("bal");
                        if (tmpRetCode!=null) {
                            detail = new RetAtsBalDetail();
                            detail.respCode = "0";
                            detail.respInfo = "交易成功";
                            detail.availBal = json.getString("bal");
                            detail.currBal = json.getString("netBal");
                            retAts.writeRetDetail(detail, p_req7506.isBatch);
                        } else {
                            detail.respCode = tmpRetCode;
                            detail.respInfo = "交易失败";
                            retAts.writeRetDetail(detail, p_req7506.isBatch);
                        }
                    }  else {
                    detail.respCode = "-1";
                    detail.respInfo = "查询失败";
                    retAts.writeRetDetail(detail, p_req7506.isBatch);
                  }
                } else {
                    detail.respCode = "-1";
                    detail.respInfo = "查询失败";
                    retAts.writeRetDetail(detail, p_req7506.isBatch);
                }

        retAts.writeEnd();
        return retAts;
    }

    @Override
    public RetAts7505HisBalQry d_7505_BalQry(Req7505HisBalQry p_req7505) throws Exception {
        return null;
    }
}
